package com.kedacom.freedomLabel.view.activity.printer;

import android.content.DialogInterface;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.Gravity;
import android.view.View;
import android.widget.FrameLayout;

import com.kedacom.freedomLabel.Options;
import com.kedacom.freedomLabel.R;
import com.kedacom.freedomLabel.base.BaseActivity;
import com.kedacom.freedomLabel.base.BasicCache;
import com.kedacom.freedomLabel.databinding.ActivityPrinterLabelSelectBinding;
import com.kedacom.freedomLabel.databinding.LabelTemplate6030Binding;
import com.kedacom.freedomLabel.databinding.LabelTemplate60Binding;
import com.kedacom.freedomLabel.label.annotation.TemplateName;
import com.kedacom.freedomLabel.model.bean.Template;
import com.kedacom.freedomLabel.model.db.AppDatabase;
import com.kedacom.freedomLabel.model.db.dao.TemplateDao;
import com.kedacom.freedomLabel.view.fragment.ListDataFragment;
import com.kedacom.freedomLabel.viewmodel.printer.LabelSelectViewModel;
import com.kedacom.lego.annotation.ContentView;
import com.kedacom.lego.annotation.OnMessage;
import com.kedacom.lego.annotation.ViewModel;


@ContentView(R.layout.activity_printer_label_select)
@ViewModel(LabelSelectViewModel.class)
public class LabelSelectActivity extends BaseActivity<ActivityPrinterLabelSelectBinding, LabelSelectViewModel> {

    Template mSelectItem;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        TemplateDao dao = AppDatabase.getInstance().templateDao();
        ListDataFragment<Template> fragment = ListDataFragment.newInstance(dao);
        transaction.add(mBinding.dataListFragment.getId(), fragment);
        transaction.commit();

        Template template = BasicCache.getInstance().getTemplate();
        if (template == null) {
            // 未选择应用模板
            showDialog("请选择打印标签模板并应用", getString(R.string.dialog_positive),
                    (dialog, which) -> {
                        beep();
                        dialog.dismiss();
                    });
        } else {
            showDialog("当前应用的标签模板\r\n" + template.type, getString(R.string.dialog_positive),
                    (dialog, which) -> {
                        beep();
                        dialog.dismiss();
                    });
        }
    }

    @OnMessage(value = Options.MSG_SELECT_ITEM, discard = true)
    public void receiveItemClickMessage(Template item) {
        if (item == null) return;
        mSelectItem = item;
        mBinding.labelTemplatePreview.removeAllViews();
        int labelWidth= item.width;
        int labelHeight = item.height;

        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
                labelWidth * 6, labelHeight * 6);
        params.gravity = Gravity.CENTER;

        switch (item.type) {
            case TemplateName.LABEL_0:

                LabelTemplate6030Binding preview6030 = DataBindingUtil.inflate(getLayoutInflater(),
                        R.layout.label_template_60_30, null, false);
                mBinding.labelTemplatePreview.addView(preview6030.getRoot(), params);
                break;
            case TemplateName.LABEL_1:

                LabelTemplate60Binding preview = DataBindingUtil.inflate(getLayoutInflater(),
                        R.layout.label_template_60, null, false);
                mBinding.labelTemplatePreview.addView(preview.getRoot(), params);
                break;
            case TemplateName.LABEL_2:
                break;
        }
    }

    @OnMessage(value = Options.MSG_RECOVERY_ITEM_DETAIL, discard = true)
    public void recoveryDetailLayout() {
        mBinding.labelTemplatePreview.removeAllViews();
    }

    // 应用
    public void onUsageClick(View view) {
        beep();
        if (mSelectItem != null) {
            String typeName = mSelectItem.type;
            showDialog("确认应用-" + typeName + "模板打印", getString(R.string.dialog_positive), new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    beep();
                    if (which == DialogInterface.BUTTON_POSITIVE) {
                        BasicCache.getInstance().saveTemplate(mSelectItem);
                        showToast("打印模板应用成功", TOAST_SUCCESS);
                    }
                }
            });
        }
    }

    // 返回
    public void onBackClick(View view) {
        beep();
        finish();
    }
}